package hivi.vn.hivigold.activities;


import hivi.vn.hivigold.R;
import hivi.vn.hivigold.database.Preferences;
import hivi.vn.hivigold.database.WebService;
import hivi.vn.hivigold.fragment.GoldPriceFagment;
import hivi.vn.hivigold.fragment.HomeFragment;
import hivi.vn.hivigold.fragment.HomeFragment.HomeListener;
import hivi.vn.hivigold.fragment.PartnerFragment;
import hivi.vn.hivigold.fragment.ReportFragment;
import hivi.vn.hivigold.fragment.SearchItemFragment;
import hivi.vn.hivigold.fragment.SearchItemFragment.SearchListener;
import hivi.vn.hivigold.fragment.VoucherFragment;
import hivi.vn.hivigold.objects.Item;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.Window;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.Toast;


public class Main extends FragmentActivity implements HomeListener, SearchListener{
	Button btnHome;
	Button btnLogout;
	Button btnCheck;
	SearchView sv;
	int currentFeature = -1;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_layout);
		
		btnHome = (Button) findViewById(R.id.btnHome);
		btnLogout = (Button) findViewById(R.id.btnLogout);
		btnCheck = (Button) findViewById(R.id.btnCheck);
		sv = (SearchView) findViewById(R.id.txtCheck);
		
		btnHome.setOnClickListener(onClick);
		btnLogout.setOnClickListener(onClick);
		btnCheck.setOnClickListener(onClick);
		
		HomeFragment hf = new HomeFragment();
		hf.setHomeListener(this);
		getSupportFragmentManager().beginTransaction().add(R.id.fragment1, hf).commit();
		
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	}
	
	OnClickListener onClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.btnHome && currentFeature!=-1)
			{
					HomeFragment hf = new HomeFragment();
					hf.setHomeListener(Main.this);
					getSupportFragmentManager().beginTransaction().replace(R.id.fragment1, hf).commit();
					currentFeature = -1;
			}
			else if (v.getId() == R.id.btnLogout)
			{
				Preferences.editPreferenceValue(getBaseContext(), "CurrentUser", "*");
				Intent i = new Intent(getBaseContext(), LoginActivities.class);
				startActivity(i);
				finish();
			}else if(v.getId() == R.id.btnCheck)
			{
				hideKeyboard(sv);
				if(sv.getQuery().toString().length()>0)
				{
					CheckTask checkTask = new CheckTask();
					checkTask.execute(sv.getQuery().toString());
				}else
				{
					Toast.makeText(getBaseContext(),R.string.warning_check_voucher, Toast.LENGTH_LONG).show();
				}
			}
		}
	};
	
	void showKeyboard() {
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null)
			imm.showSoftInput(sv, 0);
	}

	void hideKeyboard(View v) {
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null)
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}
	
	@Override 
	public void HomeClickListener(int feature) {
		// TODO Auto-generated method stub
		FragmentManager fm= getSupportFragmentManager();
		Fragment fg = null;
		currentFeature = feature;
		if(feature == HomeFragment.FEATURE_SEARCHITEM)
		{
			fg = new SearchItemFragment();
			((SearchItemFragment)fg).setListener(this);
		}else if(feature ==HomeFragment.FEATURE_GOLDPRICE)
		{
			fg = new GoldPriceFagment();
		}else if(feature ==HomeFragment.FEATURE_PARTNER)
		{
			fg = new PartnerFragment();
		}else if(feature ==HomeFragment.FEATURE_SETTING)
		{
			Intent i = new Intent(this, SettingActivities.class);
			startActivityForResult(i, 1);
			return;
		}else if(feature ==HomeFragment.FEATURE_REPORT)
		{
			fg = new ReportFragment();
		}else if(feature ==HomeFragment.FEATURE_TRANSACTION)
		{
			fg = new VoucherFragment();
		}
		fm.beginTransaction().replace(R.id.fragment1, fg).commit();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void saleClickListener(Item i) {
		// TODO Auto-generated method stub
		FragmentManager fm= getSupportFragmentManager();
		Fragment fg = new VoucherFragment();
		Bundle bundle = new Bundle();
		bundle.putSerializable(VoucherFragment.ITEM_SELECTED, i);
		fg.setArguments(bundle);
		fm.beginTransaction().replace(R.id.fragment1,fg).commit();
	}
	
	class CheckTask extends AsyncTask<String, Void, String>
	{
		ProgressDialog dialog;
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(Main.this);
			dialog.setMessage(getResources().getString(R.string.alert_waiting));
			dialog.show();
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
			builder.setTitle(R.string.lbl_check);
			if (result.equals("1")) {
				builder.setMessage(R.string.lbl_check_result_success);
			} else if(result.equals("0")){
				builder.setMessage(R.string.lbl_check_result_fail);
			} else
			{
				builder.setMessage(R.string.lbl_check_result_not_exist);
			}
			builder.setNegativeButton(R.string.btn_close, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stubt
					dialog.dismiss();
					getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
				}
			});
			builder.create().show();
		}
		
		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return WebService.checkVoucher(Main.this, params[0]);
		}
		
	}
	
}
